﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain
{
	using Falcon.Core.Authorization;
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Validation;

	public abstract class EntityBase<T> : IEntity<T>
	{
		/// <summary>
		/// Gets or sets the id.
		/// </summary>
		/// <value>The id.</value>
		public virtual int Id
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can view.
		/// </summary>
		/// <value><c>true</c> if this instance can view; otherwise, <c>false</c>.</value>
		public virtual bool CanView
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can view any.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance can view any; otherwise, <c>false</c>.
		/// </value>
		public virtual bool CanViewAny
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can create.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance can create; otherwise, <c>false</c>.
		/// </value>
		public virtual bool CanCreate
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can update.
		/// </summary>
		/// <value><c>true</c> if this instance can update; otherwise, <c>false</c>.</value>
		public virtual bool CanUpdate
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can update any.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance can update any; otherwise, <c>false</c>.
		/// </value>
		public virtual bool CanUpdateAny
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can delete.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance can delete; otherwise, <c>false</c>.
		/// </value>
		public virtual bool CanDelete
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance can delete any.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance can delete any; otherwise, <c>false</c>.
		/// </value>
		public virtual bool CanDeleteAny
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether to skip validation.
		/// </summary>
		/// <value><c>true</c> if skip validation; otherwise, <c>false</c>.</value>
		public virtual bool SkipValidation
		{
			get; 
			set;
		}

		/// <summary>
		/// Determines the permission.
		/// </summary>
		/// <param name="role">The role.</param>
		public virtual void DeterminePermission(string role)
		{
			this.CanView = Has(role, UserPermissionActions.View);
			this.CanViewAny = Has(role, UserPermissionActions.ViewAny);
			this.CanCreate = Has(role, UserPermissionActions.Create);
			this.CanUpdate = Has(role, UserPermissionActions.Update);
			this.CanUpdateAny = Has(role, UserPermissionActions.UpdateAny);
			this.CanDelete = Has(role, UserPermissionActions.Delete);
			this.CanDeleteAny = Has(role, UserPermissionActions.DeleteAny);
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public virtual Validator<T> Rules()
		{
			return new Validator<T>();
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public abstract void Validate();

		/// <summary>
		/// Determines whether this instance has the permission for the specified role.
		/// </summary>
		/// <param name="role">The role.</param>
		/// <param name="action">The action.</param>
		/// <returns>
		/// 	<c>true</c> if this instance has the permission for the specified role; otherwise, <c>false</c>.
		/// </returns>
		protected bool Has(string role, UserPermissionActions action)
		{
			return new PermissivePrincipalPermission(role, action).Authorized();
		}
	}
}